<?php
/***
 *产品模板
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Api;

use App\Jobs\Php2PyImg;
use Illuminate\Support\Facades\Queue;
use App\Services\ProdTempCategoryService\ProdTempCategoryTool;
use App\Services\ProdTempCreationMasksService\CreationMaskApiService;
use App\Services\ProductTempService\ProductTempApiService;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;
class ProductTempController extends Controller
{
    protected $productTempApiSer;
    protected $creationMaskApiSer;
    protected $productTempTool;
    /**
     * ProductTempController constructor.
     */
    public function __construct(ProductTempApiService $productTempApiSer,
                                ProductTempTool $productTempTool,
                                CreationMaskApiService $creationMaskApiSer)
    {
        $this->productTempApiSer=$productTempApiSer;
        $this->creationMaskApiSer=$creationMaskApiSer;
        $this->productTempTool=$productTempTool;
    }

    /****
     * 获取创作启动蒙板，产品分类列表
     * @param Request $request
     * @return mixed
     */
    public function creationMasks(Request $request)
    {
        $v=array(
            'per_page'=>'required|integer|min:1',
            'page'=>'required|integer|min:1'
        );
        $validator=ProductTempTool::store($request,$v);
        $page=intval($request->input('page'));
        $per_page=$request->input('per_page');
        if ($validator->fails())
        {
            if ($validator->errors()->first('per_page')) {
                $per_page=6;
            }
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result= GeneralTool::createResult('', 'M02001');
                }
            }
            if(isset($result)){
                return Response::json($result);
            }
        }
        $data['per_page']=$per_page;
        $res_data=$this->creationMaskApiSer->getCreationMasks($data);
        $result= GeneralTool::createResult($res_data, 'M00000');
        return Response::json($result);

    }

    /***
     * 获取某一产品模板下创作模板
     * @param Request $request
     * @return mixed
     */
    public function getAuthoringTempByTempId(Request $request)
    {
        $v=array(
            'product_temp_id'=>'required|string|',
            'platform'=>'string|',
            'page'=>'required|integer|min:1',
            'user_id'=>'required|integer|',
            'per_page'=>'required|integer|min:1',
        );
        $validator=ProductTempTool::store($request,$v);
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $user_id=intval($request->input('user_id'));
        $product_temp_id=trim($request->input('product_temp_id'));
        $platform=trim($request->input('platform',""));
        //系统版本
        $platform_version=trim($request->input('platform_version'));
        //屏幕尺寸
        $screen_size=trim($request->input('screen_size'));
        if ($validator->fails())
        {
            if ($validator->errors()->first('per_page')) {
                $per_page=8;
            }
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result= GeneralTool::createResult('', 'M02001');
                }
            }
            if($validator->errors()->first('product_temp_id')){
                if(!empty($product_temp_id)){
                    $result= GeneralTool::createResult('', 'M12002');
                }else{
                    $result= GeneralTool::createResult('', 'M12001');
                }
            }
        }
        if(isset($result)){
            return Response::json($result);
        }
        $prod_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if($prod_exist===false){
            $result= GeneralTool::createResult('', 'M12003');
        }
        if(isset($result)){
            return Response::json($result);
        }

        $data['per_page']=$per_page;
        $data['platform']=empty($platform)?'':$platform;
        $data['per_page']=$per_page;
        $data['product_temp_id']=$product_temp_id;
        $data['user_id']=$user_id;
        $data['platform']=$platform;
        $res_data=$this->productTempApiSer->getAuthoringTempByTempId($data);
        $result= GeneralTool::createResult($res_data, 'M00000');
        return Response::json($result);

    }

    /***
     * 获取某一产品模板分类下的产品模板Tag
     * @param Request $request
     * @return mixed
     */
    public function getAuthoringTempTagsByTempCategoryId(Request $request)
    {
        $v=array(
            'product_temp_category_id'=>'required|integer|min:1',
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
        );
        $validator=ProductTempTool::store($request,$v);
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $temp_category_id=intval($request->input('product_temp_category_id'));
        if ($validator->fails())
        {
            if ($validator->errors()->first('per_page')) {
                $per_page=8;
            }
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result= GeneralTool::createResult('', 'M02001');
                }
            }
            if($validator->errors()->first('product_temp_category_id')){
                if(!empty($temp_category_id)){
                    $result= GeneralTool::createResult('', 'M12004');
                }else{
                    $result= GeneralTool::createResult('', 'M12005');
                }
            }
            if(isset($result)&&sizeof($result)!==0){
                return Response::json($result);
            }
        }
        $prod_temp_category_exist=ProdTempCategoryTool::productTempCategoryExistsByProdTempCategoryId($temp_category_id);
        if($prod_temp_category_exist===false){
            $result= GeneralTool::createResult('', 'M12006');
            return Response::json($result);
        }
        $data['per_page']=$per_page;
        $data['temp_category_id']=$temp_category_id;
        $res_data=$this->productTempApiSer->getAuthoringTempTagsByTempCategoryId($data);
        $result= GeneralTool::createResult($res_data, 'M00000');
        return Response::json($result);

    }

    /***
     * 获取产品模板标签下的产品模板列表
     * @param Request $request
     * @return mixed
     */
    public function getAuthoringTempsByTempTagId(Request $request)
    {
        $v=array(
            'product_temp_category_id'=>'required|integer|min:1',
            'page'=>'required|integer|min:1',
            'tag_id'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
        );
        $validator=ProductTempTool::store($request,$v);
        $page=intval($request->input('page'));
        $tag_id=intval($request->input('tag_id'));
        $per_page=intval($request->input('per_page'));
        $temp_category_id=intval($request->input('product_temp_category_id'));
        if ($validator->fails())
        {
            if ($validator->errors()->first('per_page')) {
                $per_page=8;
            }
            if ($validator->errors()->first('tag_id')) {
                $result= GeneralTool::createResult('', 'M12007');
            }
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    $result= GeneralTool::createResult('', 'M02001');
                }
            }
            if($validator->errors()->first('product_temp_category_id')){
                if(!empty($temp_category_id)){
                    $result= GeneralTool::createResult('', 'M12004');
                }else{
                    $result= GeneralTool::createResult('', 'M12005');
                }
            }
            if(isset($result)&&sizeof($result)>0){
                return Response::json($result);
            }
        }
        $prod_temp_category_exist=ProdTempCategoryTool::productTempCategoryExistsByProdTempCategoryId($temp_category_id);
        if($prod_temp_category_exist===false){
            $result= GeneralTool::createResult('', 'M12006');
            return Response::json($result);
        }
        $data['per_page']=$per_page;
        $data['tag_id']=$tag_id;
        $data['temp_category_id']=$temp_category_id;
        $res_data=$this->productTempApiSer->getTempListByTempTagId($data);
        $result= GeneralTool::createResult($res_data, 'M00000');
        return Response::json($result);

    }

    /***
     * 商品详情H5
     * @param Request $request
     * @return $this|void
     */
    public function goodsDetail(Request $request){
        try{
            $v=array(
                'product_id'=>'required|string|',
            );
            $validator=ProductTempTool::store($request,$v);
            $product_id=trim($request->input('product_id'));
            if ($validator->fails())
            {
                if($validator->errors()->first('product_id')){
                    return view('api.goods-detail-blank');
                }
            }
            $goods_detail=$this->productTempApiSer->goodsDetail($product_id);
            if($goods_detail===false){
                return view('api.goods-detail-blank');
            }else{
                $temp_size_format=$goods_detail['temp_size_format'];
                $product_attr_maps=$goods_detail['product_attr_maps'];
            }
            $temp_size_list=array();
            if(sizeof($temp_size_format->size_list)!==0){
                $temp_size_list[0]=$temp_size_format->size_list;
            }
            $temp_sizes=$temp_size_format->temp_sizes;
            foreach ($temp_sizes as $temp_size){
                if(sizeof($temp_size)!==0){
                    $temp_size_list[]=$temp_size;
                }
            }
            $y_size=empty($temp_size_list)?0:sizeof($temp_size_list[0]);
            $x_size=sizeof($temp_size_list);
            $t_array=array();
            for($i=0;$i<$y_size;$i++){
                $t_array[$i]=array();
            }
            for ($j=0;$j<$x_size;$j++){
                $temp_size=$temp_size_list[$j];
                for($k=0;$k<sizeof($temp_size);$k++){
                    $t_array[$k][$j]=$temp_size[$k];
                }
            }
            return view('api.goods-detail-page')->with('temp_size_format',$temp_size_format)
                ->with('product_attr_maps',$product_attr_maps)
                ->with('temp_size_list',$t_array);

        }catch (\Exception $e){
            Log::info($e);
            return view('api.goods-detail-blank');
        }
    }

    /***
     * 产品模板详情H5页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function tempDetail(Request $request){
        try{
            $v=array(
                'temp_id'=>'required|string|',
                'colour_id'=>'required|integer|',
            );
            $product_temp_id=trim($request->input('temp_id'));
            $colour_id=intval($request->input('colour_id'));
            $validator=ProductTempTool::store($request,$v);
            if ($validator->fails())
            {
                if($validator->errors()->first('temp_id')){
                    return view('api.goods-detail-blank');
                }
            }
            $product_temp_name=$this->productTempTool->getProductTempNameByProductTempId($product_temp_id,1);
            $temp_colours=$this->productTempApiSer->tempDetailColours($product_temp_id);
            $temp_detail=$this->productTempApiSer->tempDetail($product_temp_id);
            if($temp_detail===false){
                return view('api.goods-detail-blank');
            }else{
                $temp_size_format=$temp_detail['temp_size_format'];
                $product_attr_maps=$temp_detail['product_attr_maps'];
            }
            $temp_size_list=array();
            if(sizeof($temp_size_format->size_list)!==0){
                $temp_size_list[0]=$temp_size_format->size_list;
            }
            $temp_sizes=$temp_size_format->temp_sizes;
            foreach ($temp_sizes as $temp_size){
                if(sizeof($temp_size)!==0){
                    $temp_size_list[]=$temp_size;
                }
            }
            $y_size=empty($temp_size_list)?0:sizeof($temp_size_list[0]);
            $x_size=sizeof($temp_size_list);
            $t_array=array();
            for($i=0;$i<$y_size;$i++){
                $t_array[$i]=array();
            }
            for ($j=0;$j<$x_size;$j++){
                $temp_size=$temp_size_list[$j];
                for($k=0;$k<sizeof($temp_size);$k++){
                    $t_array[$k][$j]=$temp_size[$k];
                }
            }
            return view('api.temp-detail')
                ->with('colour_id',$colour_id)
                ->with('temp_size_format',$temp_size_format)
                ->with('temp_colours',$temp_colours)
                ->with('product_attr_maps',$product_attr_maps)
                ->with('product_temp_name',$product_temp_name)
                ->with('temp_size_list',$t_array);
        }catch (\Exception $e){
            Log::info($e);
            return view('api.goods-detail-blank');
        }
    }

    /***
     * 产品模板一些简单参数
     * @param Request $request
     * @param $temp_id
     * @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function tempInfoH5(Request $request,$temp_id){
        try{
          if(empty($temp_id)){
              return view('api.goods-detail-blank');
          }
            $product_temp_name=$this->productTempTool->getProductTempNameByProductTempId($temp_id,1);
            $temp_detail=$this->productTempApiSer->tempDetail($temp_id);
            if($temp_detail===false){
                return view('api.goods-detail-blank');
            }else{
                $temp_size_format=$temp_detail['temp_size_format'];
                $product_attr_maps=$temp_detail['product_attr_maps'];
            }
            $temp_size_list=array();
            if(sizeof($temp_size_format->size_list)!==0){
                $temp_size_list[0]=$temp_size_format->size_list;
            }
            if(sizeof($temp_size_format->temp_sizes)!=0){
                $temp_size_list=array_merge($temp_size_list,$temp_size_format->temp_sizes);
            }
            return view('api.temp-info')
                ->with('temp_size_format',$temp_size_format)
                ->with('product_attr_maps',$product_attr_maps)
                ->with('product_temp_name',$product_temp_name)
                ->with('temp_size_list',$temp_size_list);
        }catch (\Exception $e){
            Log::info($e);
            return view('api.goods-detail-blank');
        }
    }
    /***
     * API获取商品信息
     * @param Request $request
     * @return mixed
     */
    public function productBasicInfo(Request $request){
        $v=array(
            'product_id'=>'required|string|',
        );
        $validator=ProductTempTool::store($request,$v);
        $product_id=trim($request->input('product_id'));
        if ($validator->fails())
        {
            if ($validator->errors()->first('product_id')) {
                $result= GeneralTool::createResult('', 'M03046');
                return Response::json($result);
            }
        }
        $goods_detail=$this->productTempApiSer->goodsDetail($product_id);
        if($goods_detail===false){
            $result= GeneralTool::createResult('', 'M12010');
            return Response::json($result);
        }
        $result= GeneralTool::createResult($goods_detail, 'M00000');
        return Response::json($result);
    }

    /***
     * 产品模板简要信息
     * @param Request $request
     * @return mixed
     */
    public function productTempInfo(Request $request){
        $v=array(
            'product_temp_id'=>'required|string|',
            'colour_id'=>'required|integer|',
        );
        $validator=ProductTempTool::store($request,$v);
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_id=intval($request->input('colour_id'));
        if ($validator->fails())
        {
            if ($validator->errors()->first('product_temp_id')) {
                if(!empty($product_temp_id)){
                    $result= GeneralTool::createResult('', 'M12002');
                }else{
                    $result= GeneralTool::createResult('', 'M12001');
                }
            }elseif($validator->errors()->first('colour_id')){
                $result= GeneralTool::createResult('', 'M12011');
            }
            return Response::json($result);
        }
        $product_temp_name=$this->productTempTool->getProductTempNameByProductTempId($product_temp_id,1);
        $temp_colours=$this->productTempApiSer->tempDetailColours($product_temp_id);
        $product_temp_exists=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exists||empty($temp_colours)){
            $result= GeneralTool::createResult('', 'M12003');
            return Response::json($result);
        }
        foreach ($temp_colours as $temp_colour){
            $temp_colour->product_temp_name=$product_temp_name;
        }
        $platform=$request->input("platform","");
        $isAndroid=ProductTempTool::isAndroidByPlatform($platform);
        $data['temp_colours']=$temp_colours;
        $data['temp_info_h5_url']=$isAndroid?(env("CURRENT_HTTP_DOMAIN").'/api/temp-info/'.$product_temp_id):asset('/api/temp-info/'.$product_temp_id);
        $data['selected_colour_id']=$colour_id;
        $result= GeneralTool::createResult($data, 'M00000');
        return Response::json($result);
    }
}
